package de.homelabs.bookmarkmanager.desktop.qt.domain;

import java.util.List;

import com.trolltech.qt.gui.QTreeModel;

import de.homelabs.bookmarkmanager.persistence.domain.HBookmark;

public class HBookmarkTreeModel extends QTreeModel {

	private List<HBookmark> bookmarks;
	
	public HBookmarkTreeModel(List<HBookmark> bookmarks){
		this.bookmarks = bookmarks;
	}
	
	@Override
	public Object child(Object parent, int index) {
		System.out.println("child");
	
		if (parent==null){
			System.out.println("no parent, index:"+index);
			return bookmarks.get(index);
		} else {
			System.out.println("parent: "+parent+", index: "+index);
			if (parent instanceof HBookmark){
				if (((HBookmark) parent).getChildren() != null)
					return (((HBookmark) parent).getChildAt(index));
				else
					return null;
					
			} else 
				return null;
		}
	}

	@Override
	public int childCount(Object parent) {
		System.out.println("childcount: "+bookmarks.size()+", parent: "+parent);
		if (parent == null)
			return bookmarks.size();
		else {
			if (parent instanceof HBookmark){
				if (((HBookmark) parent).getChildren() != null)
					return (((HBookmark) parent).getChildren().size());
				else
					return 0;
					
			} else 
				return 0;
		}
			
	}

	@Override
	public String text(Object value) {
		System.out.println("text for objekt: "+value);
		if (value instanceof HBookmark){
			return ((HBookmark) value).getTitle();
		} else 
			return null;
	}

	
}
